package com.site.dao.impl.master;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.site.dao.master.ProvinceDao;
import com.site.dto.master.ProvinceBizBean;
import com.site.model.master.ProvinceInfo;
import com.site.util.base.dao.BaseDaoImpl;
import com.site.util.extremetable.QueryPage;

/**
 * 省市Dao
 * @author lee
 * @date 2013年11月7日
 */
@Repository
public class ProvinceDaoImpl extends BaseDaoImpl implements ProvinceDao{
 
	/* (non-Javadoc)
	 * @see com.site.dao.impl.master.ProvinceDao#getProvinces(com.site.dto.master.ProvinceBizBean)
	 */
	@Override
	public List<ProvinceInfo> getProvinces(ProvinceBizBean vo) {
		String hql = "from ProvinceInfo ";
		Map<String, Object> params = new HashMap<String, Object>();
		List<ProvinceInfo> objList = this.findByHQL(hql,params);
		if(null == objList || objList.size()==0)
			return null;
		return objList;
	}

	@Override
	public List<ProvinceBizBean> getProvincesAndCitys(ProvinceBizBean vo,
			QueryPage queryPage) {
		String sql = "SELECT * FROM (SELECT c.`id`,c.`name` FROM `c_code_city` c UNION ALL SELECT p.`id`,p.`name` FROM `c_code_province` p) b";
		List<Object[]> objects = null;
		if(queryPage != null){
			objects = super.findBySQL(queryPage, sql);
		} else {
			objects = super.findBySQL(sql);
		}
		if(null != objects && objects.size() > 0){
			List<ProvinceBizBean> provinceBizBeans = new ArrayList<ProvinceBizBean>();
			for (Object[] objects2 : objects) {
				if(null != objects2){
					ProvinceBizBean provinceBizBean = new ProvinceBizBean();
					provinceBizBean.setId(objects2[0] != null ? Long.parseLong(objects2[0]+""):null);
					provinceBizBean.setName(objects2[1] != null ? objects2[1]+"":null);
					provinceBizBeans.add(provinceBizBean);
				}
			}
			if(null != provinceBizBeans && provinceBizBeans.size() > 0){
				return provinceBizBeans;
			}
		}
		return null;
	}
}
